package bar.dark.lee.fan;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.common.BDUtil;

public class FanUpdateAdapter extends ArrayAdapter<FanData> implements OnItemClickListener
{
	private Context context;
	private ArrayList<FanData> list;
	private int flag;
	private String shape;
	
	public FanUpdateAdapter(Context context, int textViewResourceId,
			ArrayList<FanData> list, int flag)
	{
		super(context, textViewResourceId, list);
		this.context = context;
		this.list = list;
		this.flag = flag;
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
		shape = " " + shape;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View v = convertView;
		
		if (v == null)
		{
			LayoutInflater li = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			v = li.inflate(R.layout.list_item_fan_update, null);
		}
		
		FanData data = list.get(position);
		
		TextView date = (TextView) v.findViewById(R.id.fanUpdateCheckListDate);
		TextView money = (TextView) v.findViewById(R.id.fanUpdateCheckListMoney);
		
		date.setText(BDUtil.getDateButton(data.getDate()));
		
		if (data.getType().equals(BDConstents.CREDIT_TYPE))
		{
			money.setTextColor(context.getResources().getColor(R.color.postmoney));
		}
		else if (flag == FanUpdateListActivity.OUT)
		{
			money.setTextColor(context.getResources().getColor(R.color.outmoney));
		}
		else if (flag == FanUpdateListActivity.IN)
		{
			money.setTextColor(context.getResources().getColor(R.color.inmoney));
		}
		
		money.setText(BDUtil.getWon(data.getMoney()) + shape);
		
		return v;
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		
		if (flag == FanUpdateListActivity.OUT)
		{
			Intent intent = new Intent(context, FanOutInsertActivity.class);
			intent.putExtra(FanOutInsertActivity.INSERT_FLAG, FanOutInsertActivity.UPDATE_DETAIL);
			intent.putExtra(FanOutInfoActivity.FAN_INFO_DATA, list.get(pos));
			context.startActivity(intent);
		}
		if (flag == FanUpdateListActivity.IN)
		{
			Intent intent = new Intent(context, FanInInsertActivity.class);
			intent.putExtra(FanInInsertActivity.FLAG, FanInInsertActivity.UPDATE_DETAIL);
			intent.putExtra(FanInInfoActivity.FAN_INFO_DATA, list.get(pos));
			context.startActivity(intent);
		}
	}
}
